<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="130px">
      <el-form-item label="考生编号" prop="candidateId">
        <el-input
          v-model="queryParams.candidateId"
          placeholder="请输入考生编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          ref="input"
          @keyup.enter.native="handleQueryIdCard"
        />
      </el-form-item>
      <el-form-item label="准考证号" prop="examNumber">
        <el-input
          v-model="queryParams.examNumber"
          placeholder="请输入准考证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="体能测评结果" prop="physicalTestResult">
        <el-select v-model="queryParams.physicalTestResult" placeholder="请选择体能测评结果" clearable>
          <el-option
            v-for="dict in selectList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:physical:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:physical:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['system:physical:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:physical:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:physical:import']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-view"
          size="mini"
          @click="clickIdCard"
        >识别</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-refresh-right"
          size="mini"
          @click="handleGenerateDbPhysicalList"
          v-hasPermi="['system:physical:edit']"
        >生成递补资格审查名单</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="physicalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="" align="center" prop="id" />-->
      <el-table-column label="考生编号" align="center" prop="candidateId" width="120"/>
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="gender" />
      <el-table-column label="身份证号" align="center" prop="idCard" width="200"/>
      <el-table-column label="准考证号" align="center" prop="examNumber" width="120"/>
      <el-table-column label="行测成绩" align="center" prop="administrativeAptitudeScore" />
      <el-table-column label="申论成绩" align="center" prop="essayScore" />
      <el-table-column label="专业科目成绩" align="center" prop="professionalSubjectScore" width="120"/>
      <el-table-column label="总成绩" align="center" prop="totalScore" />
      <el-table-column label="专业技能测试成绩" align="center" prop="professionalSkillTestScore" width="140"/>
      <el-table-column label="报考单位" align="center" prop="appliedUnit" width="180"/>
      <el-table-column label="报考职位" align="center" prop="appliedPosition" width="200"/>
      <el-table-column label="招考人数" align="center" prop="recruitmentNumber" />
      <el-table-column label="面试比例" align="center" prop="interviewRatio" />
      <el-table-column label="排名" align="center" prop="ranking" />
      <el-table-column label="毕业院校" align="center" prop="graduationSchool" width="180"/>
      <el-table-column label="是否增加专业技能测试" align="center" prop="hasProfessionalSkillTest" width="150"/>
      <el-table-column label="职位类别" align="center" prop="positionCategory" />
      <el-table-column label="体能测评结果" align="center" prop="physicalTestResult" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.physicalTestResult != null" :type="scope.row.physicalTestResult == '未通过' ? 'danger' : 'success'">{{scope.row.physicalTestResult}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="modifyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:identity:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:identity:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改四级联考体能测评对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="考生编号" prop="candidateId">
              <el-input v-model="form.candidateId" placeholder="请输入考生编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <el-input v-model="form.gender" placeholder="请输入性别" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="身份证号" prop="idCard">
              <el-input v-model="form.idCard" placeholder="请输入身份证号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="准考证号" prop="examNumber">
              <el-input v-model="form.examNumber" placeholder="请输入准考证号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报考单位" prop="appliedUnit">
              <el-input v-model="form.appliedUnit" placeholder="请输入报考单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报考职位" prop="appliedPosition">
              <el-input v-model="form.appliedPosition" placeholder="请输入报考职位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="毕业院校" prop="graduationSchool">
              <el-input v-model="form.graduationSchool" placeholder="请输入毕业院校" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="增加专业技能测试" prop="hasProfessionalSkillTest">
              <el-input v-model="form.hasProfessionalSkillTest" placeholder="请输入是否增加专业技能测试" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="职位类别" prop="positionCategory">
              <el-input v-model="form.positionCategory" placeholder="请输入职位类别" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编号" prop="registrationId">
              <el-input v-model="form.bianhao" placeholder="请输入编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="体能测评结果" prop="physicalTestResult">
              <el-radio v-model="form.physicalTestResult" label="通过">通过</el-radio>
              <el-radio v-model="form.physicalTestResult" label="未通过">未通过</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考生照片" prop="photoPath">
              <div v-if="form.idCard">
                <img :src="photoUrl + form.idCard + '.jpg'" style="vertical-align: bottom; width: 100px; height: 140px;" />
                <!--                <el-button style="margin-left:20px" type="danger" @click="deletephoto()">删除</el-button>-->
              </div>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-checkbox v-model="upload.updateSupport" />更新已存在数据
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPhysical, getPhysical, delPhysical, addPhysical, updatePhysical, generateDbPhysicalList } from "@/api/system/physical";
import { getToken } from "@/utils/auth";
import axios from "axios";
import { listIdentity } from '@/api/system/identity'

export default {
  name: "Physical",
  data() {
    return {
      photoUrl: process.env.VUE_APP_BASE_API + "/profile/",
      barcodeUrl: process.env.VUE_APP_BASE_API + "/profile/barcode/",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      selectList: [{
        value: '通过',
        label: '通过'
      }, {
        value: '未通过',
        label: '未通过'
      }],
      // 四级联考体能测评表格数据
      physicalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        candidateId: null,
        name: null,
        idCard: null,
        examNumber: null,
        appliedUnit: null,
        appliedPosition: null,
        hasProfessionalSkillTest: null,
        positionCategory: null,
        physicalTestResult: null,
        modifyTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        idCard: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        examNumber: [
          { required: true, message: "准考证号不能为空", trigger: "blur" }
        ],
      },
      // 导入相关参数
      upload: {
        title: "体能测评数据导入",
        open: false,
        updateSupport: false,
        isUploading: false,
        headers: { Authorization: "Bearer " + getToken() },
        url: process.env.VUE_APP_BASE_API + "/system/physical/importData"
      }
    };
  },
  mounted() {
    this.getList()
    this.$nextTick(() => {
      this.$refs.input.focus()
    })
  },
  created() {
    this.$nextTick(() => {
      this.$refs.input.focus()
    })
  },
  methods: {
    clickIdCard() {
      axios.get('http://127.0.0.1:19196/readcard')
        .then((res) => {
          let idCard = res.data.certNumber
          console.log(idCard)
          this.queryParams.pageNum = 1;
          this.queryParams.idCard = idCard;
          this.loading = true;
          listPhysical(this.queryParams).then(response => {
            this.physicalList = response.rows;
            this.total = response.total;
            this.loading = false;
            // 检查结果数量是否为1，如果是则弹出修改窗口
            if (this.physicalList.length === 1) {
              this.handleUpdate(this.physicalList[0]);
            }else{
              this.$message.error('未查询到此人');
            }
            this.queryParams = {}
            this.getList()
          });
        })
    },
    /** 查询四级联考体能测评列表 */
    getList() {
      this.loading = true;
      listPhysical(this.queryParams).then(response => {
        this.physicalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        candidateId: null,
        name: null,
        gender: null,
        idCard: null,
        examNumber: null,
        administrativeAptitudeScore: null,
        essayScore: null,
        professionalSubjectScore: null,
        totalScore: null,
        professionalSkillTestScore: null,
        appliedUnit: null,
        appliedPosition: null,
        recruitmentNumber: null,
        interviewRatio: null,
        ranking: null,
        graduationSchool: null,
        hasProfessionalSkillTest: null,
        positionCategory: null,
        isAlternate: null,
        qualificationReviewResult: null,
        identityVerificationResult: null,
        physicalTestResult: null,
        finalResult: null,
        modifyTime: null
      };
      this.resetForm("form");
      this.$nextTick(() => {
        this.$refs.input.focus()
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleQueryIdCard() {
          // let idCard = res.data.certNumber
          this.queryParams.pageNum = 1;
          // this.queryParams.idCard = idCard;
          this.loading = true;
          listPhysical(this.queryParams).then(response => {
            this.physicalList = response.rows;
            this.total = response.total;
            this.loading = false;
            // 检查结果数量是否为1，如果是则弹出修改窗口
            if (this.physicalList.length === 1) {
              this.handleUpdate(this.physicalList[0]);
            }
          });
      this.resetForm("queryForm");

    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加四级联考体能测评";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPhysical(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改四级联考体能测评";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (!this.form.physicalTestResult) {
            this.$modal.msgError("请录入体能测评结果再保存");
            return;
          }
          if (this.form.id != null) {
            updatePhysical(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPhysical(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
      this.$nextTick(() => {
        this.$refs.input.focus()
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除四级联考体能测评编号为"' + ids + '"的数据项？').then(function() {
        return delPhysical(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/physical/export', {
        ...this.queryParams
      }, `四级联考体能测评数据_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "四级联考体能测评数据导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('system/physical/importTemplate', {
      }, `四级联考体能测评数据导入模板.xlsx`);
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },

    /** 生成递补体能测试名单 */
    handleGenerateDbPhysicalList() {
      this.$modal.confirm('确认生成递补资格审查名单？此操作将根据不合格考生，从成绩合格名单中递补考生。').then(() => {
        return generateDbPhysicalList();
      }).then(response => {
        this.$modal.msgSuccess(response.msg || "生成递补资格审查名单成功");
        this.getList();
      }).catch(() => {});
    }
  }
};
</script>
